/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v1.2 copyright Comine.com 20101211S22
#include "MStdLib.h"
#include "MString.h"
#include "MIrrklang.h"
#include "MCGLSoundInfo.h"


//******************************************************
//**  MCGLSoundInfo class
//******************************************************
void MCGLSoundInfo::ClearObject(void)
	{
	mDevice=NULL;
	mSound=NULL;
	}


////////////////////////////////////////////////
MCGLSoundInfo::MCGLSoundInfo(void)
	{  ClearObject();  }


////////////////////////////////////////////////
MCGLSoundInfo::~MCGLSoundInfo(void)
	{  Destroy();  }


////////////////////////////////////////////////
bool MCGLSoundInfo::Create(ISoundEngine *engine,const char *filename)
	{
	Destroy();

	if(engine==NULL)
		{
		Destroy();
		return false;
		}

	// Get Reference to engine
	mDevice=engine;
	mDevice->grab();

	if(mFileName.Create(filename)==false)
		{
		Destroy();
		return false;
		}

	return true;
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::Destroy(void)
	{
	if(mSound!=NULL)
		{
		mSound->stop();
		mSound->drop();
		mSound=NULL;
		}

	if(mDevice!=NULL)
		{
		mDevice->drop();
		mDevice=NULL;
		}

	mFileName.Destroy();
	ClearObject();
	return true;
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::Play(bool loop)
	{
	if(mSound!=NULL)
		{
		mSound->stop();
		mSound->drop();
		mSound=NULL;
		}

	mSound=mDevice->play2D(mFileName.Get(),loop,false,true);
	if(mSound==NULL)
		{
		return false;
		}

	return true;
	}


/////////////////////////////////////////////////
bool MCGLSoundInfo::Play3D(float x,float y,float z,bool loop)
	{
	if(mSound!=NULL)
		{
		mSound->stop();
		mSound->drop();
		mSound=NULL;
		}

	mSound=mDevice->play3D(mFileName.Get(),vec3df(x,y,z)
			,loop,false,true);
	if(mSound==NULL)
		{
		return false;
		}

	return true;
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::Stop(void)
	{
	if(mSound==NULL)
		{
		return false;
		}

	mSound->setIsPaused(true);
	return true;
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::Continue(void)
	{
	if(mSound==NULL)
		{
		return false;
		}

	mSound->setIsPaused(false);
	return true;
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::IsPaused(void)
	{
	if(mSound==NULL)
		{
		// No Sound means that it should be considered paused
		return true;
		}

	return mSound->getIsPaused();
	}

////////////////////////////////////////////////
bool MCGLSoundInfo::IsFinished(void)
	{
	if(mSound==NULL)
		{
		return false;
		}

	return mSound->isFinished();
	}


////////////////////////////////////////////////
bool MCGLSoundInfo::SetVolume(float volume)
	{
	if(mSound==NULL)
		{
		return false;
		}

	mSound->stop();
	return true;
	}


///////////////////////////////////////////////
bool MCGLSoundInfo::SetPan(float val)
	{
	if(mSound==NULL)
		{
		return false;
		}

	mSound->setPan(val);
	return true;
	}


///////////////////////////////////////////////
int MCGLSoundInfo::GetPlayLength(void)
	{
	if(mSound==NULL)
		{
		return false;
		}

	return mSound->getPlayLength();
	}
	

///////////////////////////////////////////////
int MCGLSoundInfo::GetPlayPosition(void)
	{
	if(mSound==NULL)
		{
		return false;
		}

	return mSound->getPlayPosition();
	}


///////////////////////////////////////////////
const char *MCGLSoundInfo::GetFileName(void)
	{
	return mFileName.Get();
	}

